package com.hrt.manage.product.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.ProType;
import com.hrt.bean.ProductInfor;
import com.hrt.manage.product.dao.ProtypeDao;
import com.hrt.manage.product.service.ProInforService;
import com.hrt.manage.product.service.ProtypeService;

@Service
public class ProtypeServiceImpl implements ProtypeService {

	@Autowired
	private ProtypeDao dao;
	@Autowired
	private ProInforService svr;

	public ProtypeServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Long totalCount() {
		// TODO Auto-generated method stub
		return dao.totalCount();
	}

	@Transactional
	@Override
	public void deleteById(Long[] ids) throws Exception {
		// TODO Auto-generated method stub
		List<ProType> delResult = dao.getByIds(ids);
		for (ProType type : delResult) {
			deleteProductRelation(svr.queryByProType(type.getId()));
			type.setIsdelete(new Integer(1));
			dao.updateEntity(type);
		}

	}

	/**
	 * 
	 * @param products
	 */
	private void deleteProductRelation(List<ProductInfor> products)
	{
		if(products!=null && products.size()>0){
			Long [] proIds=new Long[products.size()];
			int i=0;
			for(Iterator<ProductInfor> iter=products.iterator();iter.hasNext();)
			{
				proIds[i]=iter.next().getId();
				i++;
			}
			svr.deleteById(proIds);
			
		}
		
	}
	
	@Transactional
	@Override
	public void saveType(ProType type) throws Exception {
		// TODO Auto-generated method stub
		dao.saveEntity(type);

	}

	@Override
	
	public List queryByPage(int pageIndex, int pageSize) {
		List list = new ArrayList();
		list = dao.queryByPage(pageIndex, pageSize);
		return list;
	}

	@Override
	public void updateType(ProType type) {
		// TODO Auto-generated method stub
		dao.updateEntity(type);
	}

	@Override
	public ProType getTypeByid(Long typeId) {
		// TODO Auto-generated method stub
		return dao.getById(typeId);
	}

	@Override
	public List<ProType> loadTypes() {
		// TODO Auto-generated method stub
		return dao.loadList(ProType.class);
	}

	

}
